# -*- coding: utf-8 -*-
'''
Created on 14-05-2013

@author: LONG HOANG GIANG
'''
import sys, os
from urlparse import urljoin
sys.path.append(os.path.join(os.path.dirname(__file__), '../'))
import pyLib
import re
import workerpool
import json
import logging

logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', datefmt='%d/%m/%Y %H:%M:%S', )
DS = "/"

class ALOBooks():
    
    _site_url = 'http://alobooks.vn/'
    _path = "/longhoanggiang/newdata/alobooks"
    
    def __init__(self):
        self._site_category = self.getCategories()
        pyLib.createIfNotExistsPath(self._path)
    
    def getCategories(self):
        try:
            data = json.loads(pyLib.file_get_content("/longhoanggiang/tmp/alobooks-category"))
        except:
            data = []
        if data == None or len(data) == 0:
            tree = pyLib.loadWeb(self._site_url).build_tree()
            for item in tree.xpath("//h2[contains(., 'Thư viện sách')]//following-sibling::div[@class='content']/ul[@class='menu']/li[contains(@class, 'leaf')]/a".decode('utf-8')):
                name = pyLib.stringify(item)
                href = item.get('href')
                if name == href == '': continue
                href = urljoin(self._site_url, href)
                print name, href
                path = "alobooks-{0}".format(pyLib.extractText(r"/([^/]+)\.html", href, 1))
                data.append({'name': name, 'path': path, 'url': href})
            pyLib.file_put_content("/longhoanggiang/tmp/alobooks-category", json.dumps(data))
        return data

    def getChapterOfStory(self, url):
        tree = pyLib.loadWeb(url).build_tree()
        data = []
        chapterIdx = 1
        for node in tree.xpath("//div[contains(@id, 'book-navigation-')]//ul[@class='menu']/li[@class='leaf']/a"):
            href = node.get('href')
            if href == '': continue
            href = urljoin(self._site_url, href)
            data.append({'name': 'CHƯƠNG {0}'.format(chapterIdx), 'url': href})
            chapterIdx += 1
        return data

    def getDetail(self, chapter):
        
        logging.info("start get detail of {0}: {1}".format(chapter['name'], chapter['url']))
        tree = pyLib.loadWeb(chapter['url']).build_tree()
        contentNode = tree.xpath("//div[contains(@class, 'content node-book')]/div[1]")
        if len(contentNode) == 0: raise
        
        html = pyLib.Etree.tostring(contentNode[0])
#        print html
        html = pyLib.getHTMLContent(html)
        print len()

    def processAStory(self, story):
        
        chapters = self.getChapterOfStory(story['url'])
        for chapter in chapters:
            print chapter
            self.getDetail(chapter)

    def getListStoryByCat(self, cat):
        url = cat['url']
        logging.info("getListStoryByCat:{0}".format(cat['name']))
        path = self._path + DS + cat['path']
        pyLib.createIfNotExistsPath(path)
        data = []
        
        def getStoryUrl(url):
            tree = pyLib.loadWeb(url).build_tree()
            node = tree.xpath("//div[contains(., 'Đọc trực tuyến:')]/following-sibling::div[@class='field-items'][1]//a".decode('utf-8'))
            if len(node) > 0:
                url = node[0].get('href')
                if url != '':
                    url = urljoin(self._site_url, url)
                    return url
            return None
            
        
        def getCat(tree):
            for item in tree.xpath("//div[@class='view-content']/div[contains(@class, 'views-row')]//span[@class='field-content']"):
                itemNode = item.xpath(".//div[@class='listcontent']/span[@class='listtitle']/a")[0]
                name = pyLib.stringify(itemNode).strip()
                href = itemNode.get('href').strip()
                if name == '' or href == '': continue
                if href != '': href = urljoin(self._site_url, href)
                thumbNode = item.xpath(".//div[@class='listimg']//img")[0]
                thumb = thumbNode.get('src')
                if thumb != '': thumb = urljoin(self._site_url, thumb)
                href = getStoryUrl(href)
                if href == None: continue
                
                path = pyLib.extractText(r"alobooks.vn/[^/]+/(.+)\.html", href, 1).replace('/', '-')
                
                print '------------------------------------------'
                print 'Name: {0}'.format(name)
                print 'URL: {0}'.format(href)
                print 'Thumb: {0}'.format(thumb)
                print 'Path: {0}'.format(path)
                print '------------------------------------------'
                
                data.append({'name': name, 'thumb': thumb, 'url': href, 'path': path})
        
        while url != '':
            tree = pyLib.loadWeb(url).build_tree()
            
            getCat(tree)
            
            nextURLNode = tree.xpath("//ul[@class='pager']/li[@class='pager-next']/a")
            if len(nextURLNode) == 0: break
            url = nextURLNode[0].get('href').strip()
            if url != '': url = urljoin(self._site_url, url)
            
        for story in data:
            self.processAStory(story)
            
        
        pyLib.file_put_content(path + DS + "data", json.dumps(data))
        
    def process(self):
        
        categories = self.getCategories()
        pool = workerpool.WorkerPool(size=3)
        pool.map(self.getListStoryByCat, categories)
        pool.shutdown()
        pool.wait()

if __name__ == '__main__':
    
    a = ALOBooks()
#    a.getChapterOfStory('http://alobooks.vn/doc-sach-truc-tuyen/5970/mat-ma-tay-tang-tap-2-full-ha-ma.html')
#    a.getListStoryByCat(a._site_category[0])
#    a.process()
    a.getDetail({'name': '', 'url': 'http://alobooks.vn/doc-online/31652/hay-cuu-em-chuong-04.html'})
    
    logging.info("finished")
    os._exit(1)